<html>
<head><meta charset="utf-8"><title>macro expansion · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html">macro expansion</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="241723044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20expansion/near/241723044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mihir Luthra <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html#241723044">(Jun 07 2021 at 02:52)</a>:</h4>
<p>I was working on some project where I want to parse rust source files and expand specific macros (without expanding the ones nested in it). I have seen rust-analyzer does that.<br>
I was interested in understanding how does rust-analyzer do that. Can someone point me to the code in RA repo where this is done?</p>



<a name="241761320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20expansion/near/241761320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html#241761320">(Jun 07 2021 at 11:25)</a>:</h4>
<p>That's a rather complicated process. A good entry point to learn about it would be this function: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/5f7fc5ae68ff5debead06e377153e100b6e9951d/crates/ide/src/expand_macro.rs#L17-L41">https://github.com/rust-analyzer/rust-analyzer/blob/5f7fc5ae68ff5debead06e377153e100b6e9951d/crates/ide/src/expand_macro.rs#L17-L41</a>.</p>



<a name="241762303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20expansion/near/241762303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mihir Luthra <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html#241762303">(Jun 07 2021 at 11:34)</a>:</h4>
<p>Thanks, will check it out :)</p>



<a name="241762787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20expansion/near/241762787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mihir Luthra <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html#241762787">(Jun 07 2021 at 11:39)</a>:</h4>
<p>Also, I was checking out hir_expand crate. Is it usable externally without RA specific data?</p>



<a name="241764778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20expansion/near/241764778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html#241764778">(Jun 07 2021 at 11:59)</a>:</h4>
<p><code>hir_expand</code> is not intended to be used alone. <code>hir</code> is the crate you would use, and it should be usable for this, but the APIs are still experimental and can change all the time</p>



<a name="241764972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20expansion/near/241764972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html#241764972">(Jun 07 2021 at 12:00)</a>:</h4>
<p>also note that to expand one macro, you basically need to expand all, because name resolution is intertwined with macro expansion and you can't know in advance which macros you need to expand to be able to resolve the one macro you're actually interested in</p>



<a name="241768194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20expansion/near/241768194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mihir Luthra <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html#241768194">(Jun 07 2021 at 12:27)</a>:</h4>
<p><span class="user-mention" data-user-id="129457">@Florian Diebold</span> , I am<br>
not sure I understand completely. When I bring my cursor over a macro, it does expand that specific macro without expanding nested ones. So how can I not know in advance which macro to expand?</p>



<a name="241768229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20expansion/near/241768229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html#241768229">(Jun 07 2021 at 12:27)</a>:</h4>
<p>it has already expanded all macros in the crate at that point</p>



<a name="241768366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20expansion/near/241768366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html#241768366">(Jun 07 2021 at 12:28)</a>:</h4>
<p>I don't mean that you can't request the expansion for a specific macro call, just that getting that expansion isn't as easy as "parse the file, expand that specific macro"</p>



<a name="241768582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20expansion/near/241768582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mihir Luthra <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20expansion.html#241768582">(Jun 07 2021 at 12:30)</a>:</h4>
<p>Okay, that makes sense. I suppose this would still work for me. I will check the hir crate. Thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>